package com.iweb.tmall.interceptor;

import com.iweb.tmall.global.CustomException;
import com.iweb.tmall.global.ErrorEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class ApiAuthInterceptor implements HandlerInterceptor {
    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String remotAuth = request.getHeader("Auth");
        /*HttpSession session = request.getSession();
        String localAuth = (String)session.getAttribute("Auth");*/
        //用过key获取redis中存储的用户的token，
        String localAuth = stringRedisTemplate.opsForValue().get(remotAuth);
        if (remotAuth!= null && localAuth!=null){
            if (remotAuth.equals(localAuth)){
                return true;
            }
        }
        throw new CustomException(ErrorEnum.IDENTITY_VERIFICATION_FAILS);
    }
}
